

//
//  DetailViewController.m
//  GitBucket
//
//  Created by gdm on 15/11/7.
//  Copyright © 2015年 ios25. All rights reserved.
//

#import "DetailViewController.h"
#import "AllREADMEViewController.h"
#import "CodeTableViewController.h"
@interface DetailViewController ()
{
    //下一个界面需要的
    NSString *url_string;
    NSArray *data_arr;
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightLayoutConstraint;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *actView;


@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSLog(@"self.dataDic::::::::::%@",self.dataDic);
    //取出字典中的数据赋值
    
    _reposLabel.text = _dataDic[@"name"];
    _owerLabel.text = _dataDic[@"owner"][@"login"];
    _watchLabel.text = [NSString stringWithFormat:@"%@",_dataDic[@"watchers_count"]];
    _starLabel.text = [NSString stringWithFormat:@"%@",_dataDic[@"stargazers_count"]];
    _forkLabel.text = [NSString stringWithFormat:@"%@",_dataDic[@"forks"]];
    NSString *descriptionStr =_dataDic[@"description"];
    if (![descriptionStr isKindOfClass:[NSNull class]]) {
        _detailLabel.text = _dataDic[@"description"];

    }
    _updateLabel.text = _dataDic[@"updated_at"];
    
    //以下内容在多线程中执行
    NSBlockOperation *aOperation = [NSBlockOperation blockOperationWithBlock:^{
        //请求文件全部内容
        
        NSString *contents_strurl = _dataDic[@"contents_url"];
        NSString *contents_strurl2 = [contents_strurl substringToIndex:contents_strurl.length - 8];
        NSLog(@"contents_strurl2:%@",contents_strurl2);
        
        NSString *contents_strurl2_long = [NSString stringWithFormat:@"%@?client_id=7222dd6497b25737c70c&client_secret=b507ccb75cd6b99d6759a79ee0b406d13294f4fa",contents_strurl2];
        NSURL *contents_url = [NSURL URLWithString:contents_strurl2_long];
        NSData *data = [NSData dataWithContentsOfURL:contents_url];
        if (data != nil) {
            //如果数据不为空则进行网络请求
            NSArray *contents_arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            data_arr = [NSArray arrayWithArray:contents_arr];
            NSLog(@"contents_arr:%@",contents_arr);
            for (NSDictionary *dic in contents_arr) {
                NSString *name = dic[@"name"];
                if ([name isEqualToString:@"README.md"]) {
                    
                    NSURL *download_url = [NSURL URLWithString:dic[@"download_url"]];
                    
                    NSString *str = [NSString stringWithContentsOfURL:download_url encoding:NSUTF8StringEncoding error:nil];
                    url_string = dic[@"html_url"];
                    NSLog(@"str:%@",str);
                    //有关UI的回到主线程更新UI
                    NSOperationQueue  *mainQueue = [NSOperationQueue mainQueue];
                    [mainQueue addOperation:[NSBlockOperation blockOperationWithBlock:^{
                        _textView.text = str;
                        [_actView stopAnimating];
                        //设置高度约束
//                        _heightLayoutConstraint = [NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:200];
                        _heightLayoutConstraint.constant = 250;
                    }]];
                    
                }
            }
        }else{
            //数据为空则显示提示框
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您的仓库为空,或者无网络连接" message:@"请重新配置这个仓库或重新连接网络" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *aAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){
            }];
            [alertController addAction:aAction];
            //以模态视图呈现
            [self presentViewController:alertController animated:YES completion:nil];
        }

    }];
    //创建队列
    NSOperationQueue *textViewQueue = [NSOperationQueue new];
    textViewQueue.maxConcurrentOperationCount = 1;
    [textViewQueue addOperation:aOperation];
    
    }

-(void)viewWillAppear:(BOOL)animated
{
    //显示底部
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"viewAll"]) {
        AllREADMEViewController *arvc = segue.destinationViewController;
        arvc.repoLabel.text = self.reposLabel.text;
        arvc.ownerLabel.text = self.owerLabel.text;
        arvc.url_string = [NSString stringWithFormat:@"%@?client_id=7222dd6497b25737c70c&client_secret=b507ccb75cd6b99d6759a79ee0b406d13294f4fa",url_string];
    }else
    {
        CodeTableViewController *ctvc = segue.destinationViewController;
        ctvc.repoLabel.text = self.reposLabel.text;
        ctvc.ownerLabel.text = self.owerLabel.text;
        ctvc.data_arr = data_arr;
    }
    
}


@end
